package day52.org.service.impl;

import day52.basic.constants.Constant;
import day52.basic.service.impl.BaseServiceImpl;
import day52.org.domain.Department;
import day52.org.mapper.DepartmentMapper;
import day52.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
        this.update(department);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY ,key = "'tree'" )
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY ,key = "'tree'" )
    public void update(Department department) {
        String dirPath = "/";
        //如果父级对象等于null，自己属于顶级部门
        if(department.getParent() == null){
            dirPath = dirPath + department.getId();
        }else{
            //查询父部门
            Department parent = departmentMapper.findOne(department.getParent().getId());
            dirPath = parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY ,key = "'tree'" )
    public void patchDelete(List<Long> ids) {
        departmentMapper.patchDelete(ids);
    }

    /**
     * 无限级部门树如何查询
     * 1.嵌套查询
     *      每次要查全表数据库操作频繁，性能低
     * 2.嵌套结果
     *       只能查询两级
     * 3.全部查出来重新组装数据【掌握】
     *      也会慢，但是后面可以使用缓存来解决
     * @return
     */
    @Override
    @Cacheable(cacheNames = Constant.DEPT_TREE_KEY ,key = "'tree'" ) //xxx::xxx
    public List<Department> queryTree() {

        return getDeptTreeFromDb();
    }

   //获取部门树提成一个方法
    private List<Department> getDeptTreeFromDb() {
        //查询出所有的dept
        List<Department> allDepts = departmentMapper.findAll();

        //结果集
        List<Department> resultDept = new ArrayList<>();

        for (Department dept : allDepts) {
            //找出所有的爹
            if(dept.getParent_id() == null){
                resultDept.add(dept);
            }else{
                //找出所有的儿子塞到爹的children里 dept在这里就是子部门了
                for (Department parent : allDepts) {
                    if(parent.getId().equals(dept.getParent_id())){
                        //把儿子塞到爹的children里去
                        parent.getChildren().add(dept);
                    }
                }
            }
        }
        return resultDept;
    }
}
